package mx.netbrain.controller;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

import mx.netbrain.model.Tree;

import org.apache.log4j.Logger;
import org.primefaces.model.DefaultStreamedContent;
import org.primefaces.model.StreamedContent;

public class FileDownloadController {

	private static StreamedContent fileDownload;
	private static final Logger logger = Logger
			.getLogger(FileDownloadController.class);

	public static StreamedContent fileDownloadController(Tree tree) {
		InputStream stream = null;
		try {
			stream = new FileInputStream(tree.getRoute() + "" + tree.getName()
					+ "." + tree.getExtension());
			fileDownload = new DefaultStreamedContent(stream, tree.getMime(),
					tree.getName() + "." + tree.getExtension());
			logger.info("Se descargara el archivo: " + tree.getName());
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			logger.error(e);
		}
		return fileDownload;

	}
}
